package com.qh.servlet;


import com.google.gson.Gson;
import com.qh.dao.Jdbc;
import org.json.JSONObject;

import javax.print.Doc;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * com.qh.servlet
 * 26832
 * yjj
 * &#064;description: 按日期选择医生
 * &#064;author: lxm
 * DoctorServlet
 * &#064;create: 2024-06-08 05:50
 **/
@WebServlet("/Cdoctor")
public class DoctorServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        String date = req.getParameter("date");
        System.out.println(date);
        //根据传递的日期，查询数据库，获取医生信息
        Jdbc jdbc = new Jdbc();
        String sql ="select * from doctor where data='"+date+"'";
        Connection con = null;
        Statement smt = null;
        ResultSet rs = null;
        try{
            con = jdbc.getConnection();
            smt = con.createStatement();
            rs= smt.executeQuery(sql);
            List<Doctor> dataList = new ArrayList<>();
            while(rs.next()){
                Doctor doctor = new Doctor();
                doctor.setId(rs.getInt("id"));
                doctor.setUname(rs.getString("uname"));
                doctor.setSpecialty(rs.getString("specialty"));
                doctor.setData(rs.getString("data"));
                dataList.add(doctor);
            }
            Gson gson = new Gson();
            String json = gson.toJson(dataList);

            // 将JSON字符串写入响应体
            resp.getWriter().write(json);
            // 关闭流
            resp.getWriter().flush();
            resp.getWriter().close();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                jdbc.close(con,smt,rs);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

    }
}
class Doctor{
    int id;
    String uname;
    String data;
    String specialty;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

    public String getSpecialty() {
        return specialty;
    }

    public void setSpecialty(String specialty) {
        this.specialty = specialty;
    }
}
